//
//  LHTopicsDetailController.m
//  EPA
//
//  Created by 卢浩 on 16/4/22.
//  Copyright © 2016年 卢浩. All rights reserved.
//

#import "LHTopicsDetailController.h"
#import "LHTopicsDetailModel.h"
#import "LHNetWorkRequestManager.h"
#import "LHTopicsPostsInfoModel.h"

@interface LHTopicsDetailController ()<UIWebViewDelegate>

@property (weak, nonatomic) IBOutlet UILabel *addTimeLable;

@property (weak, nonatomic) IBOutlet UILabel *titleLable;

@property (weak, nonatomic) IBOutlet UIImageView *iconImgView;

@property (weak, nonatomic) IBOutlet UILabel *nameLable;

@property (weak, nonatomic) IBOutlet UIImageView *imgView;

@property (weak, nonatomic) IBOutlet UIWebView *webView;

@property (weak, nonatomic) IBOutlet UILabel *infoNumLable;

@property (weak, nonatomic) IBOutlet UILabel *likeNumLable;

@property (weak, nonatomic) IBOutlet UIButton *commentBtn;

@property (nonatomic,strong) NSTimer *timer;

@property (nonatomic,strong) NSMutableArray *commentArr;

@end

@implementation LHTopicsDetailController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.commentArr = [NSMutableArray array];
    
    self.webView.delegate = self;
    
    [self loadData];
}

- (void)loadData{
    NSDictionary *bodyDict = @{@"contentid":self.contentid/*@"555e965895f95b05320001a3"*/,@"client":@(1),@"deviceid":@"63A94D37-33F9-40FF-9EBB-481182338873",@"auth":[LHUserInfoManager shareManager].auth,@"version":@"3.0.2"};
    [LHNetWorkRequestManager requestUrl:[NSURL URLWithString:TOPICINFO_URL] Methed:POST Params:bodyDict Result:^(NSData *data, NSURLResponse *response, NSError *error) {
        if (data) {
            NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:nil];
//            NSLog(@"%@",dict);
            LHTopicsDetailModel *model = [[LHTopicsDetailModel alloc]init];
            [model setValuesForKeysWithDictionary:dict[@"data"]];
//            NSLog(@"%@,%ld,%@",model.commentlist,model.commenttotal,model.postsInfoModel.title);
            
            [self setupUIWithModel:model];
        }
    }];
}

- (void)setupUIWithModel:(LHTopicsDetailModel *)model{
    dispatch_async(dispatch_get_main_queue(), ^{
        self.navigationItem.title = model.postsInfoModel.title;
        
        self.titleLable.text = model.postsInfoModel.title;
        self.addTimeLable.text = model.postsInfoModel.addtime_f;
        self.nameLable.text = model.postsInfoModel.userinfo[@"uname"];
        [self.iconImgView sd_setImageWithURL:[NSURL URLWithString:model.postsInfoModel.userinfo[@"icon"]]];
        [self.imgView sd_setImageWithURL:[NSURL URLWithString:model.postsInfoModel.imglist.firstObject[@"imgurl"]]];
        self.infoNumLable.text = [NSString stringWithFormat:@"%d",[model.postsInfoModel.counterList[@"comment"] intValue]];
        self.likeNumLable.text = [NSString stringWithFormat:@"%d",[model.postsInfoModel.counterList[@"like"] intValue]];
        if (model.commentlist.count > 0) {
            self.commentBtn.hidden = NO;
            [self.commentBtn setTitle:[NSString stringWithFormat:@"查看近期评论(%d)",(int)model.commentlist.count] forState:(UIControlStateNormal)];
        }
        
//        NSLog(@"%@",model.postsInfoModel.html);
        if ([model.postsInfoModel.html containsString:@"<p>"]) {
            [self.webView loadHTMLString:model.postsInfoModel.html baseURL:[NSURL URLWithString:TOPICINFO_URL]];
        }else{
            self.webView.hidden = YES;
        }
        
        [self.commentArr addObjectsFromArray:model.commentlist];
    });
}

- (void)webViewDidFinishLoad:(UIWebView *)webView{
    [webView stringByEvaluatingJavaScriptFromString:
     @"var script = document.createElement('script');"
     "script.type = 'text/javascript';"
     "script.text = \"function ResizeImages() { "
     "var myimg,oldwidth;"
     "var maxwidth=380;" //缩放系数
     "for(i=0;i <document.images.length;i++){"
     "myimg = document.images[i];"
     "if(myimg.width > maxwidth){"
     "oldwidth = myimg.width;"
     "myimg.width = maxwidth;"
     "myimg.height = myimg.height * (maxwidth/oldwidth);"
     "}"
     "}"
     "}\";"
     "document.getElementsByTagName('head')[0].appendChild(script);"];
    
    [webView stringByEvaluatingJavaScriptFromString:@"ResizeImages();"];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
